package com.lanhai.zhonglai.third;

import java.util.ArrayList; 
import java.util.List;

import com.lanhai.zhonglai.beans.StationDataBean;
import com.lanhai.zhonglai.beans.StationMountBean;
import com.lanhai.zhonglai.enums.StationDataTypeEnum;
import com.lanhai.zhonglai.enums.TransDirectionEnum;
import com.lanhai.zhonglai.interfaces.IStationProtocol;
import com.lanhai.zhonglai.utils.Tools;

/**
 * 背膜 正膜同一套
 * @author zhengwei
 *
 */
public class BM  implements IStationProtocol{
	@Override
	public byte[] agvArrived(byte line, TransDirectionEnum type) {
		byte[] buf = new byte[8];
		buf[0] = 1;
		if (type == TransDirectionEnum.OnlyIn) {
			buf[2] = 1;
			buf[4] = 0;
		} else {
			buf[2] = 0;
			buf[4] = 1;
		}
		return buf;
	}

	@Override
	public byte[] applyLeave(byte line, TransDirectionEnum type) {
		byte[] buf = new byte[8];
		buf[0] = 1;
		if (type == TransDirectionEnum.OnlyIn) {
			buf[2] = 2;
			buf[4] = 0;
		} else {
			buf[2] = 0;
			buf[4] = 2;
		}
		return buf;
	}

	@Override
	public List<StationDataBean> analyseData(String ip, byte[] buf) {
		List<StationDataBean> list=new ArrayList<StationDataBean>();
		StationDataBean objin = getBean(buf, true);
		if(objin!=null) {
			objin.setCmdStr(Tools.byte2HexStr(buf));
			objin.setIp(ip);
			list.add(objin);
		}
	

		StationDataBean objout = getBean(buf, false);
		if(objout!=null) {
			objout.setCmdStr(Tools.byte2HexStr(buf));
			objout.setIp(ip);
			list.add(objout);
		}
		 return list;
	}

	StationDataBean getBean(byte[] buf, boolean in) {
		StationDataBean obj = new StationDataBean();
		if (in) {
			// 进料
			obj.setLine((byte) 1); 
		} else {
			// 出料
			obj.setLine((byte) 2); 
		}
		byte cmd=buf[2];
		if(in==false) {
			//出料
			cmd=buf[4];
		} 		 
		if(cmd==0) {
			return null;
		}
		
		if (cmd == 4) {
			// 机台异常
			obj.setEventType(StationDataTypeEnum.Error);
		} else if (cmd == 3) {
			// 允许离开
			obj.setEventType(StationDataTypeEnum.AllowLeave);
		} else if (cmd == 2) {
			// 允许传输
			obj.setEventType(StationDataTypeEnum.AllowTrans);
		} else if (cmd == 5) {
			// 允许传动
			obj.setEventType(StationDataTypeEnum.Receive);
		}else {
			obj.setEventType(StationDataTypeEnum.Info);
			// 上下料
			StationMountBean mb = new StationMountBean();
			if (in) {
				// 进料 
				int num = buf[6];
				mb.setInNum(num);
				if (num == 0) {
					mb.setAllowIn(true);
				}
			} else {
				// 出料 
				int num = buf[8];
				mb.setOutNum(num);
				if (num == 10) {
					mb.setAllowOut(true);
				}
			}
			obj.setBean(mb);
		} 
		return obj;
	}
	
	
	
	@Override
	public byte[] agvLeave(byte line, TransDirectionEnum type) {
		byte[] buf = new byte[8];
		buf[0] = line;
		buf[2] = 0;
		buf[4] = 0;
		return buf;
	}

	@Override
	public byte[] applyRecieve(byte line, TransDirectionEnum type) {
		byte[] buf = new byte[8];
		buf[0] = line;
		if (type == TransDirectionEnum.OnlyIn) {
			buf[2] = 3;
			buf[4] = 0;
		} else {
			buf[2] = 0;
			buf[4] = 3;
		}
		return buf;
	}
	@Override
	public byte[] transError(byte line, TransDirectionEnum type) {
		byte[] buf = new byte[8];
		buf[0] = 1;
		buf[2] =0;
		buf[4] = 0;
		return buf;
	}
}

